/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-07-25
 * Time: 20:21
 * To BE A Better Person-2020-3-2
 */
public class TestDemo {
    public static int convertInteger(char c){

        switch(c){
            case 'I':
                return 1;

            case 'V':
                return 5;
            case 'X':
                return 10;

            case 'L':
                return 50;

            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
        }
        return 0;
    }

    public static int romanToInt(String s) {

        int sum=0;
        for(int i=0;i<s.length()-1;i++){
            int temp=convertInteger(s.charAt(i));
            int after=convertInteger(s.charAt(i+1));
            if(after>temp){
                sum+=after-temp;
                i+=1;
                if(i==s.length()-1){
                    return sum;
                }
            }else{
                sum+=temp;
            }
        }
        sum+=convertInteger(s.charAt(s.length()-1));
        return sum;
    }

    public static void main(String[] args) {
        String s="MCMXCIV";
        int ret=romanToInt(s);
        System.out.println(ret);
    }
}
